<?php
/**
 * 《常用方法》公共类
 *
 * @author 孙晓晔
 * @version $Id: Common.class.php 1033 2010-03-25 11:26:55Z sunxy $
 */

class Common {
	function code_option($tbl, $status = '1') {
		if (is_numeric($tbl)) {
			return Common::code_option2($tbl, $status);
		}

		$key = $tbl . "_status_" . $status;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($tbl);
		if ($status == 'all') {
			$sql = "SELECT * FROM " . $tbl . " ORDER BY orderby";
		} else {
			$fmt = "SELECT * FROM %s WHERE status = '%s' ORDER BY orderby";
			$sql = sprintf($fmt, $tbl, $status);
		}
		$result = $db->query($sql);

		$ret = array('-'=>'请选择');
		while($row = $db->fetch_array($result)) {
			$ret[$row['code']] = $row['name'];
		}

		if (is_object($mc)) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	function code_option2($category, $status = '1') {
		$key = "code_" . $category . "_status_" . $status;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb(Constant::tbl_code);
		$fmt = "SELECT * FROM %s WHERE category = '%s' and status = '%s' ORDER BY orderby";
		$sql = sprintf($fmt, Constant::tbl_code, $category, $status);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array('-'=>'请选择');
		while($row = $db->fetch_array($result)) {
			$ret[$row['code']] = $row['name'];
		}

		if (is_object($mc)) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	function code($tbl, $code) {
		if (empty($code)) {
			return '';
		}

		if (is_numeric($tbl)) {
			return Common::code2($tbl, $code);
		}

		$key = $tbl . "_code_" . $code;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($tbl);
		$fmt = "SELECT * FROM %s WHERE code = '%s'";
		$sql = sprintf($fmt, $tbl, $code);
		// debug($sql);
		$result = $db->query($sql);

		$data = $db->fetch_array($result);
		if (is_object($mc)) {
			$mc->set($key, $data, 0, _strtotime("1 day"));
		}

		return $data;
	}

	function code2($category, $code) {
		if (empty($code)) {
			return '';
		}

		$key = $category . "_code_" . $code;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb(Constant::tbl_code);
		$fmt = "SELECT * FROM %s WHERE category = '%s' and code = '%s'";
		$sql = sprintf($fmt, Constant::tbl_code, $category, $code);
		// debug($sql);
		$result = $db->query($sql);

		$data = $db->fetch_array($result);
		if (is_object($mc)) {
			$mc->set($key, $data, 0, _strtotime("1 day"));
		}

		return $data;
	}

	function id_option($tbl, $status = '1') {
		$key = $tbl . "_status_" . $status;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($tbl);
		if ($status == 'all') {
			$sql = "SELECT * FROM " . $tbl;
		} else {
			$fmt = "SELECT * FROM %s WHERE status = '%s'";
			$sql = sprintf($fmt, $tbl, $status);
		}
		// debug($sql);
		$result = $db->query($sql);

		$ret = array('-'=>'请选择');
		while($row = $db->fetch_array($result)) {
			$ret[$row['id']] = $row['name'];
		}

		if (is_object($mc)) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	function id($tbl, $id) {
		if (empty($id)) {
			return '';
		}

		$key = $tbl . "_id_" . $id;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		debug($tbl);
		$db = newdb($tbl);
		$fmt = "SELECT * FROM %s WHERE id = '%s'";
		$sql = sprintf($fmt, $tbl, $id);
		debug($sql);
		$result = $db->query($sql);

		$data = $db->fetch_array($result);
		if (is_object($mc)) {
			$mc->set($key, $data, 0, _strtotime("1 day"));
		}

		return $data;
	}

	function x($tbl, $field, $value) {
		if (empty($value)) {
			return '';
		}

		$key = $tbl . "_" . $field . "_" . $value;
		debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($tbl);
		$fmt = "SELECT * FROM %s WHERE %s = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $tbl, $field, $value);
		debug($sql);
		$result = $db->query($sql);

		$data = $db->fetch_array($result);
		if (is_object($mc)) {
			$mc->set($key, $data, 0, _strtotime("1 day"));
		}

		return $data;
	}

	function name($tbl, $name) {
		if (empty($name)) {
			return '';
		}

		$key = $tbl . "_name_" . md5($name);
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($tbl);
		$fmt = "SELECT * FROM %s WHERE name = '%s'";
		$sql = sprintf($fmt, $tbl, $name);
		// debug($sql);
		$result = $db->query($sql);

		$data = $db->fetch_array($result);
		if (is_object($mc)) {
			$mc->set($key, $data, 0, _strtotime("1 day"));
		}

		return $data;
	}

	function name_id($tbl, $id) {
		if (empty($id)) {
			return '';
		}

		$row = Common::id($tbl, $id);
		
		if ($row['status'] == '1' || $row['status'] == '9') {
			return $row['name'];
		}
		
		return sprintf("%s%s", $row['name'], $row['status']);		
	}
	
	function code_id($tbl, $id) {
		if (empty($id)) {
			return '';
		}

		$row = Common::id($tbl, $id);

		return $row['code'];
	}

	function name_x($tbl, $field, $value) {
		if (empty($value)) {
			return '';
		}

		$row = Common::x($tbl, $field, $value);
		
		if ($row['status'] <> '1') {
			return sprintf("%s%s", $row['name'], $row['status']);
		}

		return $row['name'];
	}

	function name_code($tbl, $code) {
		if (empty($code)) {
			return '';
		}

		$row = Common::code($tbl, $code);
		
		if ($row['status'] <> '1') {
			return sprintf("%s%s", $row['name'], $row['status']);
		}

		return $row['name'];
	}

	function dept_option() {
		return $this->code_option(Constant::tbl_dept);
	}

	function age($dd) {
		if (empty($dd) || $dd == "0000-00-00") {
			$dd = '1990-01-01';
		}
		
		$t1 = strtotime($dd);
		$ret = date('Y') - date('Y', $t1);

		$t2 = mktime(0, 0, 0, date("m"), date("d"), date("Y") - $ret);
		if ($t2 > $t1) {
			$ret++;
		}

		return $ret;
	}

	function cc($status = 4) {
		$key = "admin.priv_status_" . $status;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$ret = array();
		$ret = array('-'=>'请选择');

		$db = newdb(Constant::tbl_priv);
		$fmt = "SELECT * FROM %s WHERE status = '%s' ORDER BY orderby";
		$sql = sprintf($fmt, Constant::tbl_priv, $status);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$ret[$row['action']] = $row['action'];
		}

		if (is_object($mc)) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	function action($status = 3) {
		$key = "admin.priv_status_" . $status;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$data = array();

		$db = newdb(Constant::tbl_priv);
		$fmt = "SELECT * FROM %s WHERE status = '%s'";
		$sql = sprintf($fmt, Constant::tbl_priv, $status);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$data[] = $row['cc'] . "?do=" . $row['action'];
		}

		if (is_object($mc)) {
			$mc->set($key, $data, 0, _strtotime("1 day"));
		}

		return $data;
	}
	
	function emp($id) {
		if (empty($id)) {
			return '';
		}
		
		return Common::id(Constant::tbl_emp, $id);
	}
}
?>